<?php

/**
 * $Id: IndexController.php 423 2008-05-30 14:42:41Z dakl $
 * Strona glowna
 *
 */
class PaymentController extends MainController implements ControllerInterface {

    //o - order
    //v - order vector
    //t - total items to select
    public $tableConfig = array(
        'payment_table_main' => array(
            'o' => array(
                'id' => 'payments.id_payments',
                'serial' => 'product.code',
                'name' => 'product_description.name',
                '_' => 'id'
            ),
            'v' => 'malejaco',
            't' => 10
        ),
        'infopts_table' => array(
            'o' => array(
                'id' => 'image_map.id_image_map',
                'name' => 'image_map_description.title',
                '_' => 'id'
            ),
            'v' => 'malejaco',
            't' => 10
        )
    );

    /**
     * strona glowna
     *
     */
    public function IndexAction($param) {
        $this->AddDescription(' ');
        $this->AddTitle(' ');
        $lang = Registry::Get('default_language_id');
        $dal = PaymentsDAL::GetDalDataObj(array(
                    'tableData' => $this->tableParams['payment_table_main'],
                ));
        $dal->addCondition('user_type', Payments::TYPE_SHOP);
        $dal->addCondition('id_payee', Registry::Get('shopObj')->getId());

        if (Request::IsPost() && Request::Check('id_manufacturer')) {
            $data = Request::GetAllPost();
            SessionProxy::SetValue('product_filter', serialize($data));
            $this->addRedirect(Router::GenerateUrl('productIndex', array('product' => 'index')), 0);
        }

        if (SessionProxy::IsSetValue('product_filter')) {
            $data = SessionProxy::GetValue('product_filter');
            $data = unserialize($data);

            $filtering = false;

            if ($data['id_manufacturer'] > 0) {
                $dal->addCondition('id_manufacturer', $data['id_manufacturer']);
                $filtering = true;
            }

            if ($data['id_category'] > 0) {
                $dal->addCondition('id_category', $data['id_category']);
                $filtering = true;
            }

            if (strlen($data['code']) > 0) {
                $dal->addCondition('code', $data['code']);
                $filtering = true;
            }

            if (strlen($data['name']) > 0) {
                $dal->addCondition('product_description.name', "'%" . $data['name'] . "%'", 'LIKE');
                $filtering = true;
            }

            if ($filtering) {
                $this->smarty->assign('filter', $data);
            } else {
                SessionProxy::ClearValue('product_filter');
            }
        }

        $list = PaymentsDAL::GetResult($dal);
        $dal->setCount(true);
        $dal->setLimit('');
        $count = PaymentsDAL::GetResult($dal);

        $this->smarty->assign('list', $list);
        $this->smarty->assign('c', $count);
    }

    public function InvoiceAction($param) {
        $this->AddDescription(' ');
        $this->AddTitle(' ');
        $this->SetNoRender();
        $invoiceNumber = Config::Get('invoice_number');
        $invoiceAddress = Config::Get('invoice_address');
        $invoicePerson = Config::Get('invoice_person');

        $this->smarty->assign('invoiceNumber', ++$invoiceNumber[0]);
        $this->smarty->assign('invoiceAddress', $invoiceAddress[0]);
        $this->smarty->assign('invoicePerson', $invoicePerson[0]);


        $paymentId = $param['id'];
        $dalObj = PaymentsDAL::GetDalDataObj();
        $dalObj->setId($paymentId);
        $paymentObj = PaymentsDAL::GetById($dalObj);
        $this->smarty->assign('paymentObj', $paymentObj);

        $this->smarty->assign('shopObj', Registry::Get('shopObj'));

        $v2 = (23 / 100);
        $v2 = ($v2 + 1);

        $priceTotal = ($paymentObj->getBalance());
        $priceNet = ($paymentObj->getBalance() - ($paymentObj->getBalance()*0.23));
        $vatAmount = ($paymentObj->getBalance() * 0.23);
        
                $this->smarty->assign('priceTotal', $priceTotal);
        $this->smarty->assign('priceNet', $priceNet);
        $this->smarty->assign('vatAmount', $vatAmount);
        
        
        $priceArray = explode(',', $priceTotal);

        $wordPriceArray = str_split($priceArray[0]);

        $wordPrice = "";
        foreach ($wordPriceArray as $string) {
            if ($string == "0") {
                $string = '"0"';
            }
            $wordPrice .= " " . $this->T($string);
        }

        $this->smarty->assign('wordPrice', $wordPrice);

        if (isset($priceArray[1])) {
            if (strlen($priceArray[1]) > 2) {
                $priceArray[1] = (10 * $priceArray[1]);
            }
            $this->smarty->assign('wordPriceDec', $priceArray[1] . '/100 gr');
        } else {
            $this->smarty->assign('wordPriceDec', '00/100 gr');
        }
        
        $bodyPlain = $this->smarty->fetch('partial/Payment/Invoice.tpl');

        include_once('../lib/mpdf/mpdf.php');
	$pdf = new mPDF();
	$pdf->debug = true;
	$pdf->WriteHTML($bodyPlain);
	echo $pdf->Output();
        
        
    }

    public function ProductsAction($param) {
        $this->AddDescription(' ');
        $this->AddTitle(' ');
        $lang = Registry::Get('default_language_id');
        $dal = ProductDAL::GetDalDataObj(array(
                    'tableData' => $this->tableParams['product_table_main'],
                    'join' => array(
                        'ProductDescription' => ' LEFT JOIN product_description ON(product.id_product = product_description.id_product AND product_description.id_language = ' . Registry::Get('default_language_id') . ')',
                        'ProductShop' => ' INNER JOIN product_shop ON(product.id_product = product_shop.id_product)'
                    )
                ));
        $dal->addCondition('id_shop', Registry::Get('shopObj')->getId());

        if (Request::IsPost() && Request::Check('id_manufacturer')) {
            $data = Request::GetAllPost();
            SessionProxy::SetValue('product_filter', serialize($data));
            $this->addRedirect(Router::GenerateUrl('productIndex', array('product' => 'index')), 0);
        }

        if (SessionProxy::IsSetValue('product_filter')) {
            $data = SessionProxy::GetValue('product_filter');
            $data = unserialize($data);

            $filtering = false;

            if ($data['id_manufacturer'] > 0) {
                $dal->addCondition('id_manufacturer', $data['id_manufacturer']);
                $filtering = true;
            }

            if ($data['id_category'] > 0) {
                $dal->addCondition('id_category', $data['id_category']);
                $filtering = true;
            }

            if (strlen($data['code']) > 0) {
                $dal->addCondition('code', $data['code']);
                $filtering = true;
            }

            if (strlen($data['name']) > 0) {
                $dal->addCondition('product_description.name', "'%" . $data['name'] . "%'", 'LIKE');
                $filtering = true;
            }

            if ($filtering) {
                $this->smarty->assign('filter', $data);
            } else {
                SessionProxy::ClearValue('product_filter');
            }
        }

        $list = ProductDAL::GetResult($dal);
        $dal->setCount(true);
        $dal->setLimit('');
        $count = ProductDAL::GetResult($dal);

        $this->smarty->assign('list', $list);
        $this->smarty->assign('c', $count);
    }

    /**
     * Metoda wspolna
     *
     */
    public function preDispatch($param) {
        $this->RunShared('Auth');
        $this->Run($param);
        $this->smarty->assign('param_from_url', $param);
    }

    /**
     * 
     *
     */
    public function postDispatch($param) {
        
    }

}
